/**
 * 
 */
package com.demo2do.fms.admin.dao.business;

import java.util.Date;
import java.util.List;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.annotation.Execute;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;

/**
 * @author downpour
 *
 */
@Dao("hibernate")
public interface OutshipmentDao {
    
    /**
     * 生成外包指令派车
     * 
     * @param loadNumbers
     * @param operator
     * @param operateTime
     */
    @Execute("UPDATE Shipment SET status = 2, operator = :operator, lastModifiedDate = :lastModifiedDate WHERE disabled = false AND loadNumber IN (:loadNumbers)")
    public void generate(@Variable("loadNumbers") List<String> loadNumbers, @Variable("operator") String operator, @Variable("lastModifiedDate") Date operateTime);
    
    /**
     * 取消外包指令派车
     * 
     * @param loadNumbers
     * @param operator
     * @param operateTime
     */
    @Execute("UPDATE Shipment SET status = 1, operator = :operator, lastModifiedDate = :lastModifiedDate WHERE disabled = false AND loadNumber in (:loadNumbers)")
    public void cancel(@Variable("loadNumbers") List<String> loadNumbers, @Variable("operator") String operator, @Variable("lastModifiedDate") Date operateTime);
    
    /**
     * 设置销单状态
     * 
     * @param financeIds
     * @param operator
     * @param operationTime
     */
    @Execute("UPDATE Finance SET balanced = true, balancedDate = :lastModifiedDate, operator = :operator, lastModifiedDate = :lastModifiedDate WHERE disabled = false AND id IN (:financeIds)")
    public void dispose(@Variable("financeIds") List<Long> financeIds, @Variable("operator") String operator, @Variable("lastModifiedDate") Date operationTime);
    
    /**
     * 设置调度指令状态为已回单确认
     * 
     * @param loadNumber
     * @param operator
     * @param lastModifiedDate
     */
    @Execute("UPDATE Shipment SET status = 3,  operator = :operator, lastModifiedDate = :lastModifiedDate WHERE disabled = false AND loadNumber = :loadNumber")
    public void accomplish(@Variable("loadNumber") String loadNumber, @Variable("operator") String operator, @Variable("lastModifiedDate") Date lastModifiedDate);
    
    /**
     * 查询未销单的Finance数量
     * 
     * @param loadNumber
     * @param companyId
     * @return
     */
    @Query("SELECT COUNT(*) FROM Finance WHERE loadNumber = :loadNumber AND balanced = false AND companyId = :companyId AND disabled = false")
    public int countBalancedStatus(@Variable("loadNumber") String loadNumber, @Variable("companyId") Long companyId);
    
}
